/*
 * @description:
 * @Author: lal
 * @Date: 2020-05-27 14:19:37
 * @LastEditors: lal
 * @LastEditTime: 2020-08-15 15:17:07
 */

import { windows } from "../windows/create-window";
const updater = require("electron-updater-xdja");
import log from "electron-log";
import { app, ipcMain } from "electron";

let checkForUpdates = () => {
  log.info(app.getVersion());
  updater.setAppConfig({
    factory: "xdja111",
    os: "Windows",
    mod: "PC",
    soft: "minitools",
    packageName: "electron.miniutools",
    updateOrigin: "http://192.168.7.236:8040",
    version: app.getVersion(),
    patchUpdate: false,
    autoDownLoad: false,
  });
  log.info("开始检测更新");
  updater.checkForUpdate();
};
ipcMain.on("exitApp", () => {
  app.exit();
});
updater.on("update-available", appInfo => {
  log.info(appInfo);
  windows["updateWindow"].once("ready-to-show", () => {
    windows["updateWindow"].show();
    windows["updateWindow"].webContents.send("appUpdateInfo", appInfo);
  });

  ipcMain.on("downloadApp", () => {
    updater.downloadUpdate();
  });
});
updater.on("update-not-available", appInfo => {
  log.info("no new");
  log.info(appInfo);
});
updater.on("download-progress", progressInfo => {
  log.info(progressInfo);
  windows["updateWindow"].webContents.send("progress", progressInfo);
});
updater.on("update-downloaded", appInfo => {
  log.info("update-downloaded");
  log.info(appInfo);
});
updater.on("error", err => {
  log.error("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
  log.error(err);
});

export default checkForUpdates;
